import {$post} from '../utils/request'
import md5 from 'js-md5'
import {message} from 'ant-design-vue';
import {useUserInfoStore} from '../store/index'

/**
 * 请求登录接口
 * @param params 登录参数
 */
export const $login = async (params) => {
    params.password = md5(md5(params.password).split('').reverse().join(''))
    let data = await $post('login', params)
    let {success, msg} = data
    if (success) {
        let {token} = data
        console.log(token)
        // 将token信息保存到store中
        useUserInfoStore.token = token
        useUserInfoStore.username = data.data.username
        useUserInfoStore.role = data.data.role
        // 将token保存到SessionStorage中
        sessionStorage.setItem("token", token)
        console.log("pina", useUserInfoStore.role)
        message.success(msg)
    } else {
        useUserInfoStore.token = ""
        useUserInfoStore.username = ""
        useUserInfoStore.role = {}
        message.error(msg)
    }
    // 在请求头中更新token信息
    // $setToken(useUserInfoStore.token)

}